package com.rs.game.player.controlers.events;

import java.util.concurrent.TimeUnit;

import com.rs.Settings;
import com.rs.cores.CoresManager;
import com.rs.game.Animation;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.item.FloorItem;
import com.rs.game.map.MapBuilder;
import com.rs.game.player.Player;
import com.rs.game.player.content.Magic;
import com.rs.game.player.controlers.Controler;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.Logger;

public class DeathEvent extends Controler {

	private int[] boundChuncks;
	private Stages stage;

	@Override
	public void start() {
		loadRoom();
	}

	public boolean login() {
		loadRoom();
		return false;
	}

	public boolean logout() {
		player.setLocation(new WorldTile(1978, 5302, 0));
		destroyRoom();
		return false;
	}

	public boolean canTakeItem(FloorItem item) {
		return false;
	}

	public boolean canEquip(int slotId, int itemId) {
		return false;
	}

	public boolean canPlayerOption1(Player target) {
		return false;
	}

	public boolean canPlayerOption2(Player target) {
		return false;
	}

	public boolean canPlayerOption3(Player target) {
		return false;
	}

	public boolean canPlayerOption4(Player target) {
		return false;
	}

	private static enum Stages {
		LOADING, RUNNING, DESTROYING
	}

	@Override
	public void sendInterfaces() {
		player.getInterfaceManager().closeCombatStyles();
		player.getInterfaceManager().closeTaskSystem();
		player.getInterfaceManager().closeSkills();
		player.getInterfaceManager().closeInventory();
		player.getInterfaceManager().closeEquipment();
		player.getInterfaceManager().closePrayerBook();
		player.getInterfaceManager().closeMagicBook();
		player.getInterfaceManager().closeEmotes();
	}

	public void loadRoom() {
		stage = Stages.LOADING;
		player.lock(); 
		CoresManager.slowExecutor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					boundChuncks = MapBuilder.findEmptyChunkBound(2, 2);
					MapBuilder.copyMap(436, 445, boundChuncks[0], boundChuncks[1], 8, 8, new int[1], new int[1]);
					player.reset();
					player.setNextWorldTile(new WorldTile(boundChuncks[0] * 8 + 16, boundChuncks[1] * 8 + 15, 0));
					player.getPackets().sendGameMessage("<col=FF0000>Touch one of the graves in order to respawn!");
					player.getPackets().sendGameMessage("<col=FF0000>Touch one of the graves in order to respawn!");
					player.getPackets().sendGameMessage("<col=FF0000>Touch one of the graves in order to respawn!");
					player.getPackets().sendGameMessage("<col=FF0000>Touch one of the graves in order to respawn!");
					player.getPackets().sendGameMessage("<col=FF0000>Touch one of the graves in order to respawn!");
					player.getPackets().sendGameMessage("<col=FF0000>Touch one of the graves in order to respawn!");
					player.getPackets().sendGameMessage("<col=FF0000>Touch one of the graves in order to respawn!");
					player.getPackets().sendGameMessage("<col=FF0000>Touch one of the graves in order to respawn!");
					WorldTasksManager.schedule(new WorldTask() {
						@Override
						public void run() {
							player.setNextAnimation(new Animation(-1));
							player.getMusicsManager().playMusic(683);
							player.getPackets().sendBlackOut(2);
							sendInterfaces();
							player.unlock(); 
							stage = Stages.RUNNING;
						}

					}, 1);
				}catch(Throwable e) {
					Logger.handle(e);
				}
			}
		});
	}

	@Override
	public boolean processMagicTeleport(WorldTile toTile) {
		return false;
	}

	@Override
	public boolean processItemTeleport(WorldTile toTile) {
		return false;
	}

	public boolean processObjectClick1(WorldObject object) {
		if (object.getId() == 5168 || object.getId() == 5167) {
			Magic.sendObjectTeleportSpell(player, true, Settings.RESPAWN_PLAYER_LOCATION);
			return false;
		}
		return true;
	}

	public WorldTile getDeathTile() {
		if (getArguments() == null || getArguments().length < 2)
			return Settings.START_PLAYER_LOCATION;
		return (WorldTile) getArguments()[0];
	}

	public boolean hadSkull() {
		if (getArguments() == null || getArguments().length < 2)
			return false;
		return (boolean) getArguments()[1];
	}

	@Override
	public void magicTeleported(int type) {
		destroyRoom();
		player.getPackets().sendBlackOut(0);
		player.getInterfaceManager().sendCombatStyles();
		player.getCombatDefinitions().sendUnlockAttackStylesButtons();
		player.getInterfaceManager().sendMsPortal();
		player.getInterfaceManager().sendSkills();
		player.getInterfaceManager().sendInventory();
		player.getInventory().unlockInventoryOptions();
		player.getInterfaceManager().sendEquipment();
		player.getInterfaceManager().sendPrayerBook();
		player.getPrayer().unlockPrayerBookButtons();
		player.getInterfaceManager().sendMagicBook();
		player.getInterfaceManager().sendEmotes();
		player.getEmotesManager().unlockEmotesBook();
		removeControler();
	}

	public void destroyRoom() {
		if (stage != Stages.RUNNING)
			return;
		stage = Stages.DESTROYING;
		CoresManager.slowExecutor.schedule(new Runnable() {
			@Override
			public void run() {
				try {
					MapBuilder.destroyMap(boundChuncks[0], boundChuncks[1], 8, 8);
				}catch(Throwable e) {
					Logger.handle(e);
				}
			}
		}, 1200, TimeUnit.MILLISECONDS);
	}

	@Override
	public void forceClose() {
		destroyRoom();
	}

}
